#!/bin/bash

. /etc/os-release
if [ "${EUID}" -ne 0 ]; then
	echo "You need to run this script as root" && exit 1
fi

apt-get update
apt-get -y upgrade

timedatectl set-timezone Asia/Kuala_Lumpur
ln -sf /usr/share/zoneinfo/Asia/Kuala_Lumpur /etc/localtime
ln -sf /usr/share/zoneinfo/zoneinfo /etc/localtime

clear && echo '';
echo 'MIT License

Copyright (c) 2021 Doctype

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.';
echo "";
echo "Before continue with the setup, we need to ask you 
some questions first";
echo "";
read -p "Enter your domain: " DOMAIN
read -p "Enter your subdomain: " SUBDOMAIN
read -p "Enter your email address: " EMAIL
read -p "Enter your username: " USERNAME
read -p "Enter your password: " PASSWORD
echo "";
echo "That all we need from you, we will start the setup
in 3 second, please wait... ";
echo "";

echo "GET_DOMAIN=$DOMAIN" > /etc/environment
echo "GET_SUBDOMAIN=$SUBDOMAIN" >> /etc/environment
echo "GET_EMAIL=$EMAIL" >> /etc/environment
echo "GET_USERNAME=$USERNAME" >> /etc/environment
echo "GET_PASSWORD=$PASSWORD" >> /etc/environment
echo 'GET_IPADDR=$(wget -qO- icanhazip.com)' >> /etc/environment
echo 'GET_PORT=$(shuf -i 1025-9999 -n 1)' >> /etc/environment
. /etc/environment

echo '/bin/false
/usr/bin/false
/usr/sbin/nologin' >> /etc/shells

echo '# Configuration file for setting system variables
kernel.printk = 3 4 1 3
net.ipv4.conf.default.rp_filter=1
net.ipv4.conf.all.rp_filter=1
net.ipv4.ip_forward=1
#net.ipv6.conf.all.forwarding=1
net.ipv4.conf.all.accept_redirects = 0
net.ipv6.conf.all.accept_redirects = 0
fs.file-max = 51200

net.core.rmem_max = 67108864
net.core.wmem_max = 67108864
net.core.netdev_max_backlog = 250000
net.core.somaxconn = 4096

net.ipv4.tcp_syncookies = 1
net.ipv4.tcp_tw_reuse = 1
# net.ipv4.tcp_tw_recycle = 0
net.ipv4.tcp_fin_timeout = 30
net.ipv4.tcp_keepalive_time = 1200
net.ipv4.ip_local_port_range = 10000 65000
net.ipv4.tcp_max_syn_backlog = 8192
net.ipv4.tcp_max_tw_buckets = 5000
net.ipv4.tcp_fastopen = 3
net.ipv4.tcp_mem = 25600 51200 102400
net.ipv4.tcp_rmem = 4096 87380 67108864
net.ipv4.tcp_wmem = 4096 65536 67108864
net.ipv4.tcp_mtu_probing = 1
net.ipv4.tcp_congestion_control = hybla' > /etc/sysctl.conf
sysctl -p &> /dev/null

# export DEBIAN_FRONTEND=noninteractive
apt-get -y -qq install build-essential
apt-get -y -qq install cmake automake
apt-get -y -qq install curl git zip

echo "" > /etc/motd
wget -q -O /etc/update-motd.d/10-uname 'https://raw.githubusercontent.com/cybertize/Doct_V1.0/default/sources/banner'
wget -q -O /etc/issue.net 'https://raw.githubusercontent.com/cybertize/Doct_V1.0/default/sources/message'

wget -q -O - 'https://raw.githubusercontent.com/cybertize/Doct_V1.0/default/packages/nginx.sh' | bash
wget -q -O - 'https://raw.githubusercontent.com/cybertize/Doct_V1.0/default/packages/dropbear.sh' | bash
wget -q -O - 'https://raw.githubusercontent.com/cybertize/Doct_V1.0/default/packages/ovpn.sh' | bash
wget -q -O - 'https://raw.githubusercontent.com/cybertize/Doct_V1.0/default/packages/squid.sh' | bash
wget -q -O - 'https://raw.githubusercontent.com/cybertize/Doct_V1.0/default/packages/stunnel.sh' | bash
wget -q -O - 'https://raw.githubusercontent.com/cybertize/Doct_V1.0/default/packages/badvpn.sh' | bash
wget -q -O - 'https://raw.githubusercontent.com/cybertize/Doct_V1.0/default/packages/webmin.sh' | bash
wget -q -O - 'https://raw.githubusercontent.com/cybertize/Doct_V1.0/default/packages/plugins.sh' | bash

apt-get -y -qq install fail2ban
cp /etc/fail2ban/jail.conf /etc/fail2ban/jail.local

# ddos-deflates
apt-get -y -qq install dnsutils net-tools tcpdump dsniff grepcidr
wget -q https://github.com/jgmdev/ddos-deflate/archive/master.zip -O /root/ddos.zip
unzip /root/ddos.zip
cd /root/ddos-deflate-master
./install.sh

systemctl enable ddos
systemctl start ddos

# iptables-persistent
DEBIAN_FRONTEND=noninteractive apt-get -y -qq install iptables-persistent
iptables -F && iptables -X && iptables -Z

cat > /etc/firewall.conf <<-EOF
-A INPUT -i lo -j ACCEPT
-A OUTPUT -o lo -j ACCEPT
-A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT

# sshd, http & https (nginx)
-A INPUT -i eth0 -p tcp --dport 22 -j ACCEPT
-A INPUT -i eth0 -p tcp --dport 80 -j ACCEPT
-A INPUT -i eth0 -p tcp --dport 443 -j ACCEPT

# dropbear
-A INPUT -i eth0 -p tcp --dport 440 -j ACCEPT
-A INPUT -i eth0 -p tcp --dport 441 -j ACCEPT
-A INPUT -i eth0 -p tcp --dport 442 -j ACCEPT

# openvpn
-A INPUT -i eth0 -p tcp --dport 1194 -j ACCEPT
-A INPUT -i eth0 -p tcp --dport 994 -j ACCEPT
-A INPUT -i eth0 -p tcp --dport 587 -j ACCEPT

# squid
-A INPUT -i eth0 -p tcp --dport 3128 -j ACCEPT
-A INPUT -i eth0 -p tcp --dport 8080 -j ACCEPT
-A INPUT -i eth0 -p tcp --dport 8000 -j ACCEPT

# stunnel
-A INPUT -i eth0 -p tcp --dport 2021 -j ACCEPT
-A INPUT -i eth0 -p tcp --dport 2022 -j ACCEPT
-A INPUT -i eth0 -p tcp --dport 2023 -j ACCEPT

# badvpn-udpgw & webmin
-A INPUT -i eth0 -p tcp --dport 7300 -j ACCEPT
-A INPUT -i eth0 -p tcp --dport 10000 -j ACCEPT

-A INPUT -m string --string "BitTorrent" --algo bm --to 65535 -j DROP
-A INPUT -m string --string "BitTorrent protocol" --algo bm --to 65535 -j DROP
-A INPUT -m string --string "peer_id=" --algo bm --to 65535 -j DROP
-A INPUT -m string --string ".torrent" --algo bm --to 65535 -j DROP
-A INPUT -m string --string "announce.php?passkey=" --algo bm --to 65535 -j DROP
-A INPUT -m string --string "torrent" --algo bm --to 65535 -j DROP
-A INPUT -m string --string "announce" --algo bm --to 65535 -j DROP
-A INPUT -m string --string "info_hash" --algo bm --to 65535 -j DROP
-A INPUT -m string --string "get_peers" --algo bm --to 65535 -j DROP
-A INPUT -m string --string "find_node" --algo bm --to 65535 -j DROP
EOF

iptables-restore < /etc/firewall.conf
cat > /etc/cron.d/firewall <<EOL
reboot   root    iptables-restore </etc/firewall.conf
EOL
